package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.common.utils.PageResult;
import com.hzit.common.utils.R;
import com.hzit.common.utils.ResponseUtil;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;
import com.hzit.vo.StudentVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// response.setContentType("application/json;charset=utf8");
		// 1. 得到cmd参数
		String cmd = request.getParameter("cmd");
		// 2. 根据参数名的不同调用不同的方法
		if(StrUtil.isNotBlank(cmd)){
			if("search".equals(cmd)){
				search(request,response);
			}
			if("add".equals(cmd)){
				add(request,response);
			}
			if("update".equals(cmd)){
				update(request,response);
			}
			if("remove".equals(cmd)){
				remove(request,response);
			}
		}
	}

	// 删除学生
	private void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 得到要删除的学生编号：
		String sids = request.getParameter("sids");
		// 2. 从数据库中删除学生
		studentService.removeStudent(sids);
		// 3. 返回
		R r = R.ok().message("删除学生成功！");
		ResponseUtil.write(response,r);
	}

	// 修改学生
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 获取要添加的学生对象
		ObjectMapper objectMapper = new ObjectMapper();
		Student student = objectMapper.readValue(request.getInputStream(), Student.class);
		// 2. 修改学生
		studentService.update(student);
		// 3. 返回
		ResponseUtil.write(response,R.ok().message("修改学生成功！"));
	}
	// 添加学生
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 获取要添加的学生对象
		ObjectMapper objectMapper = new ObjectMapper();
		Student student = objectMapper.readValue(request.getInputStream(), Student.class);
		// 2. 添加学生
		studentService.add(student);
		// 3. 返回
		ResponseUtil.write(response,R.ok().message("添加学生成功！"));
	}

	//2. 列表学生（条件查询带分页）
	private void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 第一部分：开始条件查询带分页前的准备工作（取得前端传入的参数值）
		// 2.1 得到分页参数
		int page = new Integer(request.getParameter("page"));
		int pageSize = new Integer(request.getParameter("pageSize"));
		// 2.2 得到查询对象
		ObjectMapper objectMapper = new ObjectMapper();
		StudentVO studentVO = objectMapper.readValue(request.getInputStream(), StudentVO.class);

		// 第二部分：开始分页查询
		PageResult<Student> pageResult = studentService.search(page,pageSize,studentVO);
		R r = R.ok().data("pageResult", pageResult);
		ResponseUtil.write(response,r);
	}
}
